﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FraudMitigator
{
    public partial class LetterInfoForm : Form
    {
        PrintLetters letterParent;
        MainScreen mainScrnParent;
        bool firstTimePhoneClicked = true;
        bool firstTimeZipClicked = true;
        bool ignoreClosingEvent = false;

        // TODO: Add validation checks on all textboxes
        //          - error if box blank
        //          - zip code must be 5 digits
        //          - phone must be 10 digits

        public LetterInfoForm(MainScreen _parent)
        {
            InitializeComponent();
            mainScrnParent = _parent;

            senderFirstNameTxtBox.Text = letterParent.senderFirst;
            senderLastNameTxtBox.Text = letterParent.senderLast;
            senderAddrTxtBox.Text = letterParent.senderAddr;
            senderCityTxtBox.Text = letterParent.senderCity;
            senderStateComboBox.Text = letterParent.senderState;
            senderZipTxtBox.Text = letterParent.senderZip;
            senderPhoneTxtBox.Text = letterParent.senderPhone;
            payableToTxtBox.Text = letterParent.payableTo;
            feeAmtTxtBox.Text = letterParent.feeAmount;
        }

        public LetterInfoForm(PrintLetters _parent, int letterSent)
        {
            InitializeComponent();
            letterParent = _parent;

            senderFirstNameTxtBox.Text = letterParent.senderFirst;
            senderLastNameTxtBox.Text = letterParent.senderLast;
            senderAddrTxtBox.Text = letterParent.senderAddr;
            senderCityTxtBox.Text = letterParent.senderCity;
            senderStateComboBox.Text = letterParent.senderState;
            senderZipTxtBox.Text = letterParent.senderZip;
            senderPhoneTxtBox.Text = letterParent.senderPhone;
            payableToTxtBox.Text = letterParent.payableTo;
            feeAmtTxtBox.Text = letterParent.feeAmount;

            switch (letterSent)
            {
                case 1:
                    payableToTxtBox.Enabled = false;
                    break;
                case 2:
                    payableToTxtBox.Enabled = false;
                    feeAmtTxtBox.Enabled = false;
                    break;
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            letterParent.canceled = true;
            this.Close();
        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            bool valid = ValidateInput();

            if (valid)
            {
                letterParent.senderFirst = senderFirstNameTxtBox.Text;
                letterParent.senderLast = senderLastNameTxtBox.Text;
                letterParent.senderAddr = senderAddrTxtBox.Text;
                letterParent.senderCity = senderCityTxtBox.Text;
                letterParent.senderState = senderStateComboBox.Text;
                letterParent.senderZip = senderZipTxtBox.Text;
                letterParent.senderPhone = senderPhoneTxtBox.Text;
                letterParent.payableTo = payableToTxtBox.Text;
                letterParent.feeAmount = feeAmtTxtBox.Text;

                this.Close();
            }
            else
            {
            }
        }

        private bool ValidateInput()
        {
            bool valid = true;

            try { if (feeAmtTxtBox.Enabled == true) Convert.ToInt32(feeAmtTxtBox.Text); }
            catch (FormatException) { valid = false; }            

            if (!valid)
            {
                MessageBox.Show("The fee amount provided is invalid.", "Input Error");
                feeAmtTxtBox.Focus();
                feeAmtTxtBox.SelectAll();
            }
            else if (senderZipTxtBox.Text.Length != 5)
            {
                MessageBox.Show("The zip code provided is invalid.", "Input Error");
                senderZipTxtBox.Focus();
                senderZipTxtBox.SelectAll();
            }
            else if (!valid && senderZipTxtBox.Text.Length != 5)
            {
                MessageBox.Show("The zip code and fee amount provided are invalid.", "Input Error");
                feeAmtTxtBox.Focus();
                feeAmtTxtBox.SelectAll();
            }

            ignoreClosingEvent = true;

            return valid;
        }

        private void userPhoneTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (firstTimePhoneClicked) senderPhoneTxtBox.Select(1, 1);
            firstTimePhoneClicked = false;
        }

        private void userZipTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (firstTimeZipClicked) senderZipTxtBox.Select(0, 0);
            firstTimeZipClicked = false;
        }

        private void LetterInfoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!ignoreClosingEvent) letterParent.canceled = true;
        }

        private void senderPhoneTxtBox_Validating(object sender, CancelEventArgs e)
        {

        }
    }
}
